package NewTmas.Listeners;

import NewTmas.FramesandPanels.JobMonitorOpenerFrame;
import NewTmas.FramesandPanels.JobStates;
import com.toedter.calendar.JDateChooser;
import taskmaster.auth.User;
import taskmaster.job.TaskBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: abhatnag
 * Date: 7/25/11
 * Time: 3:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class DaemonStatesOpenListener implements ActionListener {

    TaskBuilder tb;
    User user;
    JDesktopPane dtp;
    HashMap<String, String> signoffMap;
    Hashtable allRolesHash;

    public DaemonStatesOpenListener(TaskBuilder tb, User user, JDesktopPane dtp, HashMap<String, String> signoffMap, Hashtable allRolesHash) {
        this.tb=tb;
        this.user=user;
        this.dtp=dtp;
        this.signoffMap=signoffMap;
        this.allRolesHash=allRolesHash;
    }

    public void actionPerformed(ActionEvent e) {

        final JFrame frame=new JFrame("Enter a date");
        frame.getContentPane().setBackground(Color.white);


        JButton btnOk = new JButton("OK");
        frame .getContentPane().setLayout(null);
        btnOk.setBounds(123, 118, 89, 23);
        frame.getContentPane().add(btnOk);


        final SimpleDateFormat formatter=new SimpleDateFormat("MM/dd/yy");
        final JLabel todayLbl = new JLabel("This is not today's date !");
        todayLbl.setForeground(Color.RED);


        final JDateChooser datechooser=new JDateChooser();
        datechooser.setDate(Calendar.getInstance().getTime());

        datechooser.addPropertyChangeListener(new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                SimpleDateFormat formatter=new SimpleDateFormat("MM/dd/yy");


                if(formatter.format(Calendar.getInstance().getTime()).equalsIgnoreCase(formatter.format(datechooser.getDate()))) {
                    todayLbl.setVisible(false);

                }  else
                {
                    todayLbl.setVisible(true);
                }
            }
        });
        JButton todayBtn = new JButton("Today");

        JButton subDateButton = new JButton("");
        subDateButton.setIcon(new ImageIcon(JobMonitorOpenerFrame.class.getResource("/Resources/Icons/arrow_left.png")));
        subDateButton.setToolTipText("Click to subtract a date");
        JLabel lblDate = new JLabel("Date :");
        lblDate.setForeground(Color.blue)  ;



        frame.setSize(400, 200);

        frame.setVisible(true);

        lblDate.setBounds(32, 45, 34, 14);

        frame .getContentPane().add(lblDate);
        todayLbl.setBounds(88, 17, 186, 14);
        frame.getContentPane().add(todayLbl);
        btnOk.setBounds(123, 118, 89, 23);
        frame.getContentPane().add(btnOk);
        todayBtn.setBounds(243, 41, 89, 23);
        frame.getContentPane().add(todayBtn);
        subDateButton.setBounds(88, 41, 35, 23);
        frame.getContentPane().add(subDateButton);
        datechooser.setBounds(123, 42, 95, 20);
        frame.getContentPane().add(datechooser);




        subDateButton .addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                datechooser.setDate(new java.sql.Date(datechooser.getDate().getTime() - 60 * 60 * 24 * 1000));


            }
        });




        frame.setVisible(true);

        todayBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                datechooser.setDate(Calendar.getInstance().getTime());

            }
        });
        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JobStates js = new JobStates(user, datechooser.getDate(), tb, dtp,signoffMap,allRolesHash);
                js.setVisible(true);
                dtp.add(js);
                js.toFront();
                frame.dispose();
            }
        });

    }
}
